<?php
$baseUrl = Yii::app()->baseUrl;
$cs      = Yii::app()->getClientScript();

$jsFileList = array(
    'browserplus-min',
    'plupload',
    'plupload.html5',
);
foreach ($jsFileList as $file) {
    $path = $baseUrl . '/js/ext/plupload/' . $file . '.js';
    $cs->registerScriptFile($path, 1);
}
?>

<script type="text/javascript">
/*-----------------------------------------------------------
Events
-----------------------------------------------------------*/
var msgLoading = '<?php echo Yii::t('DadminModule.msg', 'loading'); ?>';

$(document).on('click', '.callAjax', function(e){
    var params = { msg: msgLoading };
    var url = $(this).attr('ref');
    
    if (url) {
        $('#null').showLoading(params);
        $.get(url, function(data){
            updateGridview();
            $('#null').dialog('close');
        });
    }
    
    return false;
})

$(document).on('click', '.callDelete', function(e){
    var params_1 = { msg: '<?php echo Yii::t('DadminModule.msg', 'delete_confirm') ?>' };
    var params_2 = { msg: msgLoading };
    var url = $(this).attr('href');
    
    $('#null').showConfirm(params_1, function(){
        if (url) {
            $('#loading').showLoading(params_2);
            $.get(url, function(data){
                updateGridview();
                $('#loading').dialog('close');
                window.location.reload();
            });
        }
    }, '');
    
    return false;
})

$('#btn-cancel, #btn-back').click(function(){
    url = '<?php echo $this->createUrl('cancel'); ?>';
    $.get(url, function(data){
        window.location = '<?php echo $this->createUrl('index'); ?>';
    });
});

$('#btn-reset').click(function(){
    window.location = $(this).attr('ref');
});

//----------- Link helper -----------
$('#dialogLinks').bind('assignLink', function() {
    $('#DSlideshow_link').val($(this).data('link'));
});
$('#assign-link').click(function(){
    var url = '<?php echo $this->createUrl('newsletterContent/assignLink') ?>';
    loadFrame(url, '#dialogLinks', 700, 450, '', 'frameLinks', '<?php echo Yii::t('DadminModule.app', 'link_helper') ?>');
    return false;
});
//----------- //Link helper -----------

/*-----------------------------------------------------------
Functions
-----------------------------------------------------------*/
function updateGridview() {
    url = '<?php echo $this->createUrl('loadMyGrid'); ?>';
    $('#myGrid').load(url)
}

function loadFrame(url, container, width, height, callBack, frameId, title) {
  var frameAttr = 'width="100%" height="100%"'
    + 'marginWidth="0" marginHeight="0"'
    + 'frameBorder="0"';
  
  myDialog = $(container).html('<iframe id="' + frameId + '" '+ frameAttr + '/>').dialog({
    modal: true,
    width: width, height: height,
    position: ['center', 55],
    draggable: false, resizable: false,
    bgiframe: true,
    close: callBack,
    title: title,
    zIndex: 10000
  });
  $("#" + frameId).attr("src", url);

  return myDialog;
};

/*-----------------------------------------------------------------------------
PLUpload
-----------------------------------------------------------------------------*/
var uploader = new plupload.Uploader({
    runtimes : 'html5',
    browse_button : 'pickfiles',
//        container : 'pluploadContainer',
    drop_element: 'pluploadContainer',
//    max_file_size : '10mb',
    url : '<?php echo $this->createUrl('upload'); ?>',
    // Specify what files to browse for
    file_data_name: 'file_upload',
    multi_selection: false
});
uploader.init();
uploader.bind('FilesAdded', function(up, file) {
    up.start();
});

uploader.bind('FileUploaded', function(up, file, res) {
    var response = jQuery.parseJSON(res.response);
    if (response.status == 200) {
        url = '<?php echo Yii::app()->baseUrl . Yii::app()->params['tempo_url'] ?>'
            + response.thumb + '?q=' + Math.floor((Math.random()*100)+1);
        $('#pickfiles').html('<img src="' + url + '" />');
        $('#DSlideshow_file').val(response.file);
        $('#DSlideshow_thumb').val(response.thumb);
        $('#slideshowUploadFileMsg').empty();
    } else {
        $('#slideshowUploadFileMsg').empty().html(response.message);
    }
});
/*-----------------------------------------------------------------------------
//PLUpload
-----------------------------------------------------------------------------*/
</script>
